package tq.com.tqstudent.ui.activity.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lib.common.base.BaseActivity;
import com.lzy.okgo.OkGo;

import butterknife.BindView;
import butterknife.OnClick;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.SimpleBaseBean;
import tq.com.tqstudent.bean.params.LoginParams;
import tq.com.tqstudent.net.ok.SimpleDialogCallback;
import tq.com.tqstudent.widgets.CountDownTextView;

/**
 * @Author rames
 * @date 2019/3/5 17:00
 */
public class BindAccountActivity extends BaseActivity {
    @BindView(R.id.verify_tv)
    CountDownTextView verifyTV;
    @BindView(R.id.phone_edit)
    TextView phoneEdit;
    @BindView(R.id.verify_pwd_edit)
    TextView verifyEdit;

    public static void show(Activity activity){
        Intent intent = new Intent(activity, BindAccountActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    public int initLayout() {
        return R.layout.activity_bind_account;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("绑定账号");
        countDownTime();
    }

    @OnClick(R.id.commit_tv)
    public void onClick(View v){
        switch (v.getId()){
            case R.id.commit_tv:
                if(check()) {
                    bindTask();
                }
                break;
        }
    }

    private void countDownTime(){
        verifyTV.setNormalText("获取验证码")
                .setCountDownText("重新获取(", ")")
                .setCloseKeepCountDown(false)//关闭页面保持倒计时开关
                .setCountDownClickable(false)//倒计时期间点击事件是否生效开关
                .setShowFormatTime(true)//是否格式化时间
                .setOnCountDownFinishListener(new CountDownTextView.OnCountDownFinishListener() {
                    @Override
                    public void onFinish() {
                    }
                })
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getVerifyTask();
                    }
                });
    }

    /**
     * 获取验证码
     */
    private void getVerifyTask(){
        String json = "{\"phone\":\"" + phoneEdit.getText().toString().trim() + "\"}";;
        OkGo.<SimpleBaseBean>post(App.getHttpUrl("/api/user/authsms"))
                .tag(this)
                .upJson(json)
                .execute(new SimpleDialogCallback(BindAccountActivity.this) {
                    @Override
                    public void handleSuccess(SimpleBaseBean data) {
                        if(data.isSuccess()){
                            verifyTV.startCountDown(Constants.COUNTDOWN_TIME);
                            showToast("验证码已发送");
                        } else {
                            showToast(data.getMsg());
                        }
                    }
                });
    }

    @Override
    public void initData() {

    }

    private boolean check(){
        String phone = phoneEdit.getText().toString().trim();
        if(phone.length() != 11){
            showToast("请输入正确的手机号");
            return false;
        }

        String sms = verifyEdit.getText().toString().trim();
        if(sms.length() != 6){
            showToast("请输入正确的6位数字验证码");
            return false;
        }

        return true;
    }

    /** 找回密码 */
    private void bindTask(){
        LoginParams params = new LoginParams();
        params.name = phoneEdit.getText().toString().trim();
        params.sms = verifyEdit.getText().toString().trim();
        String parStr = new Gson().toJson(params);

        OkGo.<SimpleBaseBean>post(App.getHttpUrl("api/user/phone"))
                .tag(this)
                .upJson(parStr)
                .execute(new SimpleDialogCallback(BindAccountActivity.this) {
                    @Override
                    public void handleSuccess(SimpleBaseBean data) {
                        if(data.isSuccess()){
                            showToast("手机号绑定成功");
                        } else {
                            showToast(data.getMsg());
                        }
                    }
                });
    }
}
